<?php

namespace App\Http\Controllers\Home;

use App\Model\Station;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use League\Flysystem\Exception;
//岗位配置
class StationController extends CommonController
{
    //

	protected  $model;
	public function __construct()
	{
		parent::__construct();
		$this->model=new Station();
	}

//	岗位列表
	public function listStation(Request $request){
		$where['company_id']=$this->company_id;
		try{
			$where['name']=isset($request->name)?$request->name:null;
			$where['status']=isset($request->status)?$request->status:null;
			$where['start_time']=isset($request->start_time)?$request->start_time:null;
			$where['end_time']=isset($request->end_time)?$request->end_time:null;
			$data=$this->model->lists($where);
		}catch (Exception $exception){
			$message=$exception->getMessage();
		}

		if(!isset($message)){
			return $this->success($data);
		}else{
			return $this->failed($message);
		}
	}




//	岗位列表接口(门店)
	public function listStation_(Request $request){
		$where['company_id']=$this->company_id;
		try{
			$data=$this->model->lists_($where);
		}catch (Exception $exception){
			$message=$exception->getMessage();
		}

		if(!isset($message)){
			return $this->success($data);
		}else{
			return $this->failed($message);
		}
	}
//	新增岗位
	public function addStation(Request $request){
		$company_id=$this->company_id;
		$this->validate($request,[
			'name' => 'required|unique:station|max:255',
			'standard_hour'=>'numeric',
			'standard_day'=>'numeric',
			'comment' => 'String',
			'status'=>'required|numeric',
			'is_store'=>'required',
			'hour_salary' =>'numeric',
		],
			['name.required'=>'岗位名称不能为空',]
		);
		DB::beginTransaction();
		try{







			$data=$request->input();
			$data['company_id']=$company_id;
			$result=$this->model->create($data);
			DB::commit();
		}catch (\Exception $exception){
			$message=$exception->getMessage();
			DB::rollBack();
		}
		if(!isset($message)){
			return $this->stored($result);
		}else{
			return $this->failed($message);
		}
	}


//	发布岗位
	public function pubStation(Request $request){

		try{
			$data=$request->only($this->model->getFillable());
			$result=$this->model->create($data);
		}catch (\Exception $exception){
			$message=$exception->getMessage();
		}
		if(!isset($message)){
			return $this->response(200,$result);
		}else{
			return $this->response(400);
		}
	}

//	编辑岗位
	public function editstation($id,Request $request){

		try{
			$result=$this->model->where('id',$id)->first();
		}catch (\Exception $exception){
			$message=$exception->getMessage();
		}
		if(!isset($message)){
			return $this->success($result);
		}else{
			return $this->failed($message);
		}
	}


//	编辑保存岗位
	public function saveStation(Request $request){

		try{

//			$this->validate($request,[
//				'name' => 'required',
//				'standard_hour'=>'required|numeric',
//				'standard_day'=>'required|numeric',
//				'hour_salary' => 'required|numeric',
//				'comment' => 'String',
//				'status'=>'required|numeric'
//			]);
			$result=$this->model->where('id',$request->id)->update($request->input());
		}catch (\Exception $exception){
			$message=$exception->getMessage();
		}
		if(!isset($message)){
			return $this->updated($result);
		}else{
			return $this->failed($message);
		}
	}
}
